package com.liu.service.impl;

import com.liu.entity.LiuOperLog;
import com.liu.dao.LiuOperLogDao;
import com.liu.service.LiuOperLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 操作日志(LiuOperLog)表服务实现类
 *
 * @author makejava
 * @since 2023-01-13 20:34:58
 */
@Service("liuOperLogService")
@Transactional
public class LiuOperLogServiceImpl implements LiuOperLogService {
    @Resource
    private LiuOperLogDao liuOperLogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param operId 主键
     * @return 实例对象
     */
    @Override
    public LiuOperLog queryById(Long operId) {
        return this.liuOperLogDao.queryById(operId);
    }

    /**
     * 分页查询
     *
     * @param liuOperLog 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<LiuOperLog> queryByPage(LiuOperLog liuOperLog, PageRequest pageRequest) {
        long total = this.liuOperLogDao.count(liuOperLog);
        return new PageImpl<>(this.liuOperLogDao.queryAllByLimit(liuOperLog, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param liuOperLog 实例对象
     * @return 实例对象
     */
    @Override
    @Async("liu-logger")
    public LiuOperLog insert(LiuOperLog liuOperLog) {
        System.out.println("开始写入");
        System.out.println("log----" + Thread.currentThread().getName());

        this.liuOperLogDao.insert(liuOperLog);
        System.out.println("写入成功");
        return liuOperLog;
    }

    /**
     * 修改数据
     *
     * @param liuOperLog 实例对象
     * @return 实例对象
     */
    @Override
    public LiuOperLog update(LiuOperLog liuOperLog) {
        this.liuOperLogDao.update(liuOperLog);
        return this.queryById(liuOperLog.getOperId());
    }

    /**
     * 通过主键删除数据
     *
     * @param operId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer operId) {
        return this.liuOperLogDao.deleteById(operId) > 0;
    }
}
